<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $login
 * @property string $password
 * @property string $email
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	 
	public $rememberMe;
	private $_identity;
	public $verifyCode;
	public $passwordRe;
	
	public $old_password;
	public $new_password;
	public $re_new_password;
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('login, password, email, passwordRe', 'required'),
			array('login', 'length', 'min'=>3, 'max'=>10),
			array('password, passwordRe', 'length', 'min'=>3, 'max'=>32),
			array('email', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, login, password, email', 'safe', 'on'=>'search'),
			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),
			array('passwordRe', 'compare', 'compareAttribute'=>'password'),
			array('email', 'email'),
			array('email, login', 'unique'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'settlements' => array(self::HAS_MANY, 'Settlements', 'id_user'),
			'settlement' => array(self::BELONGS_TO, 'Settlements', 'current_settlement'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'login' => 'Логин',
			'password' => 'Пароль',
			'passwordRe' => 'Пароль повторно',
			'email' => 'Email',
			'rememberMe' => 'Запомнить?',
			'old_password' => 'Старый пароль',
			'new_password' => 'Новый пароль',
			're_new_password' => 'Повторите новый пароль',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('login',$this->login,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->login, $this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
	
	public function validatePassword($password)
	{
		return $this->hashPassword($password)===$this->password;
	}
	public function hashPassword($password)
	{
		return md5($password);
	}
	
	public static function createSettlement($id_user = 0)
	{
		if(!Yii::app()->user->isGuest)
			$id_user = Yii::app()->user->getId();

		$stelAll = Settlements::model()->findAll();
		$coordinates = true;		
		do
		{
			$x = rand(0, 50);
			$y = rand(0, 50);
			
			foreach($stelAll as $s)
			{
				if($s->x == $x && $s->y == $y)
				{
					$coordinates = false;
					break;
				}
			}
		}
		while($coordinates == false);
		
		$setl = new Settlements;
		$setl->name = "Новая деревня";
		$setl->x = $x;
		$setl->y = $y;
		$setl->population = 1;
		$setl->approval = 100;
		$setl->id_user = $id_user;
		$setl->iscapital = 1;
		$setl->insert();
		
		$cur_set = Settlements::model()->findAll("id_user = " . $id_user);
		$id_setl = $cur_set[count($cur_set) - 1]->id;
		User::model()->updateAll(array('current_settlement' => $id_setl),'id="'.$id_user.'"');
		
		
		$fieldType = FieldType::model()->findAll();
		Fields::CreateField($fieldType[0]->id, $id_setl);
		Fields::CreateField($fieldType[0]->id, $id_setl);
		Fields::CreateField($fieldType[0]->id, $id_setl);
		
		Fields::CreateField($fieldType[1]->id, $id_setl);
		Fields::CreateField($fieldType[1]->id, $id_setl);
		Fields::CreateField($fieldType[1]->id, $id_setl);
		
		Fields::CreateField($fieldType[2]->id, $id_setl);
		Fields::CreateField($fieldType[2]->id, $id_setl);
		Fields::CreateField($fieldType[2]->id, $id_setl);
		Fields::CreateField($fieldType[2]->id, $id_setl);
		
		$builds = BuildingType::model()->findAll("id = 1 or id = 2");
		
		foreach($builds as $build)
		{
			$bld = new Buildings;
			$bld->id_building = $build->id;
			$bld->id_settlement = $id_setl;
			$bld->lvl = 1;
			$bld->insert();
		}
		
		$ResType = ResourcesType::model()->findAll();
		
		foreach($ResType as $rt)
		{
			$res = new Resources;
			$res->id_resource = $rt->id;
			$res->id_settlement = $id_setl;
			$res->count = 1000;
			$res->last_update = time();
			$res->insert();
		}
				
	}
	
}